<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>套餐列表</title>
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="apple-mobile-web-app-status-bar-style" content="black">
	<meta name="apple-mobile-web-app-capable" content="yes">
	<meta name="format-detection" content="telephone=no">
	<link rel="stylesheet" href="plugins/layui/css/layui.css" media="all" />
	<link rel="stylesheet" href="//at.alicdn.com/t/font_tnyc012u2rlwstt9.css" media="all" />
</head>
<body class="childrenBody">
<blockquote class="layui-elem-quote news_search">
	<form class="layui-form">
		<div class="layui-inline">
			<label class="layui-form-label">套餐名称</label>
			<div class="layui-input-block">
				<input type="text" class="layui-input" name="name" placeholder="请输入套餐名称">
			</div>
		</div>
		<div class="layui-inline">
			<a class="layui-btn search_btn" lay-submit="" lay-filter="queryCombo">查询</a>
		</div>
		<div class="layui-inline">
			<a class="layui-btn layui-btn-normal combosAdd_btn">添加套餐</a>
		</div>
	</form>

</blockquote>
<div class="layui-form news_list">
	<table class="layui-table" id="welfareList" lay-filter="welfares"></table>
</div>
<div id="page"></div>
<script type="text/javascript" src="js/jquery-1.11.2.min.js"></script>
<script type="text/javascript" src="plugins/layui/layui.all.js"></script>
<script type="text/javascript" src="js/user/newsList.js"></script>
<script type="text/html" id="barDemo">
	<a class="layui-btn layui-btn-xs" lay-event="edit"><i class="iconfont icon-edit"></i>编辑</a>
	<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon"></i>删除</a>
	<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="batchSend"><i class="layui-icon"></i>批量发券</a>

</script>
<script type="text/javascript">
    //重新渲染表单
    function renderForm(){
        layui.use('form', function(){
            var form = layui.form;
            form.render();
        });
    }

    layui.use(['table','form'], function(){
        var table = layui.table;
        var form = layui.form;
        //方法级渲染
        var tableIns=table.render({
            elem: '#welfareList'
            ,url: '/combos'
            ,cols: [[
//                {field:'id', title: 'ID',width:"10%"}
                {field:'name', title: '套餐名称',width:"15%" }
                ,{field:'price', title: '价格',width:"10%"}
                ,{field:'name', title: '套餐项目',width:"10%"}
                ,{field:'usePeriod', title: '有效期',width:"10%"}
                ,{field:'status', title: '状态',width:"10%",templet:function (data) {
                    if(data.status==1){
                        return "有效";
                    }else if(data.status==0){
                        return "停用";
                    }
                }}
                ,{title: '操作', align:'center', toolbar: '#barDemo'} //这里的toolbar值是模板元素的选择器

            ]]
            ,id: 'testReload'
            ,page: true
//            ,height: 600
        });

        form.on("submit(queryCombo)", function (data) {
            var name = data.field.name;
            //表格重新加载
            tableIns.reload({
                where:{
                    name:name
                }
            });
            return false;
        });

        table.on('tool(welfares)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象

            if(layEvent === 'del'){ //删除
                layer.confirm('真的删除该套餐吗', function(index){
                    layer.close(index);
                    //向服务端发送删除指令
                    var ids=obj.data.id;
                    $.ajax({
                        type: "DELETE",
                        url:"/combos/delete/"+ids,
                        success: function(data1) {
                            if(data1.code==2000){
                                top.layer.close(index);
                                top.layer.msg("删除成功");
                                obj.del();
                            }

                        } ,
                        error: function(request) {
                            alert("请求错误");
                        }
                    });
                });
            } else if(layEvent === 'edit'){ //编辑
                var ids=obj.data.id;
                editCombo(ids);
            }else if(layEvent === 'batchSend'){ //编辑
                var ids=obj.data.id;
                batchSend(ids);
            }
        });
    });

    $(window).one("resize",function(){
        $(".combosAdd_btn").click(function(){
            var index = layui.layer.open({
                title : "添加套餐",
                type : 2,
                content : "combo/combosAdd.html",
                success : function(layero, index){

                }
            })
            layui.layer.full(index);
        })
    }).resize();

    var regionsList;
    $.ajax({
        type : "GET",
        url: '/regions/getAll',
        dataType:'json',
        async:false,
        success: function(data){
            var list=data.data;
            regionsList=list;
            var html='';
            for(var i=0;i<list.length;i++){
                html += "<option value='"+list[i].id+"'>"+list[i].regionName+"</option>"
            }
            $("select[name='regionId']").append(html);
            renderForm();//表单重新渲染，要不然添加完显示不出来新的option
        }
    });


    function editCombo(id){
        var index = layui.layer.open({
            title: "编辑套餐",
            type: 2,
            content: "combo/combosEdit.html?id="+id,
            success: function (layero, index) {

            }
        });

        //改变窗口大小时，重置弹窗的高度，防止超出可视区域（如F12调出debug的操作）
        $(window).resize(function () {
            layui.layer.full(index);
        });
        layui.layer.full(index);
    }
    function batchSend(id){
        var index = layui.layer.open({
            title: "批量发券",
            type: 2,
            content: "combo/batch_send.html?id="+id,
            success: function (layero, index) {

            }
        });

        //改变窗口大小时，重置弹窗的高度，防止超出可视区域（如F12调出debug的操作）
        $(window).resize(function () {
            layui.layer.full(index);
        });
        layui.layer.full(index);
    }

</script>
</body>
</html>